package com.power.sifa.info.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.plf.common.domain.PageDataResponse;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.info.model.enumeration.Law_SearchType;
import com.power.sifa.info.model.pageInfo.Law_docKeywordPageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.power.sifa.info.model.Law_doc;
import com.power.sifa.info.dao.Law_docDao;
import com.power.sifa.info.service.Law_docService;
import com.power.sifa.info.model.pageInfo.Law_docPageInfo;

import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *  法律表业务层实现
 */
@Service
public class Law_docServiceImpl extends BaseServiceImpl implements Law_docService {

    @Autowired
    private Law_docDao dao;

    @Resource
    private EntityManager entityManager;

    @Override
    public Law_doc getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Law_doc law_doc) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (law_doc.getPrimaryKey() != null) {
        	 Law_doc law_docfDb = dao.findById(law_doc.getPrimaryKey()).orElse(null);
             if(law_docfDb != null) {
                BeanUtils.copyProperties(law_doc, law_docfDb);
                dao.save(law_docfDb);
                return;
             }
        }
        dao.save(law_doc);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Law_doc> findAll(Law_docPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }
    
    @Override
	public List<Law_doc> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<Law_doc> list) {
		dao.saveAll(list);
	}

    @Override
    public PageDataResponse pageQuery(Law_docPageInfo pageInfo) {
        // 实现条件查询，
        List<Law_doc> list = this.dao.pageQuery(pageInfo);
        // 分页
        PageDataResponse page = new PageDataResponse();
        if (CollectionUtil.isNotEmpty(list)) {
            Long total = Long.valueOf(pageInfo.getAllcount());
            int size = pageInfo.getPageSize() * pageInfo.getPageNumber();
            if (total.intValue() - size > 0) {
                page.setNextPage(pageInfo.getPageNumber() + 1);
            }
            page.setSize(pageInfo.getPageSize());
            page.setCurrent(pageInfo.getPageNumber());
            page.setTotal(total);
        } else {
            page.setSize(0);
            page.setCurrent(1);
            page.setTotal(0L);
        }
        page.setRecords(list);
        return page;

    }

    /**
     * 关键字查询
     * @param pageInfo 关键字
     * @return
     */
    @Override
    public PageDataResponse searchDocByKeyword(Law_docKeywordPageInfo pageInfo) {
        List<Law_doc> docList =  new ArrayList<>();
        if (pageInfo.getSearchType().equals(Law_SearchType.TITLESearch)) {
            docList = dao.searchDocByKeyWord(pageInfo);
        } else if (pageInfo.getSearchType().equals(Law_SearchType.DETAILSearch)) {
            docList = dao.searchDocDetailByKeyWord(pageInfo);
        } else {
            docList = dao.searchAllDocByKeyWord(pageInfo);
        }
        PageDataResponse page = new PageDataResponse();
        if (CollectionUtil.isNotEmpty(docList)) {
            Long total = Long.valueOf(pageInfo.getAllcount());
            int size = pageInfo.getPageSize() * pageInfo.getPageNumber();
            if (total.intValue() - size > 0) {
                page.setNextPage(pageInfo.getPageNumber() + 1);
            }
            page.setSize(pageInfo.getPageSize());
            page.setCurrent(pageInfo.getPageNumber());
            page.setTotal(total);
        } else {
            page.setSize(0);
            page.setCurrent(1);
            page.setTotal(0L);
        }
        page.setRecords(docList);
        return page;
    }

    /**
     * 通过id查询对应的文书数据
     * @param ids
     * @return
     */
    @Override
    public List<Law_doc> findAllDocByIds(List<String> ids) {
        return dao.findAllByIds(ids);
    }

    /**
     * 关键字查询
     *
     * @param law_docPageInfo 关键字
     * @return
     */
    @Override
    public PageDataResponse historyVersion(Law_docPageInfo law_docPageInfo) {


        // 实现条件查询，
        List<Law_doc> list = this.dao.historyVersion(law_docPageInfo);
        // 分页
        PageDataResponse page = new PageDataResponse();
        if (CollectionUtil.isNotEmpty(list)) {
            Long total = Long.valueOf(law_docPageInfo.getAllcount());
            int size = law_docPageInfo.getPageSize() * law_docPageInfo.getPageNumber();
            if (total.intValue() - size > 0) {
                page.setNextPage(law_docPageInfo.getPageNumber() + 1);
            }
            page.setSize(law_docPageInfo.getPageSize());
            page.setCurrent(law_docPageInfo.getPageNumber());
            page.setTotal(total);
        } else {
            page.setSize(0);
            page.setCurrent(1);
            page.setTotal(0L);
        }
        page.setRecords(list);
        return page;

    }
}
